local M = _G.ClientOHOH.class.new(script,_G.ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name)
local ClientOHOH = _G.ClientOHOH
local RebirthGui = ClientOHOH.load.UIManager():CreateUI("RebirthGuiNew",require(script.RebirthGui))
local Rebirth_Cfg=ClientOHOH.config.Rebirth
local ItemTool =_G.CommonOHOH.load.ItemTool();
local CallBackManager = ClientOHOH.load.CallBackManager();
local FunctionList = ClientOHOH.load.FunctionList();
local ui_list=ClientOHOH.load.UIManager().ui_list
local GlobalTipGui=ui_list.GlobalTipGui
local LanguageHelper=_G.LanguageHelper 
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local HomeGui=ui_list.HomeGui
local Good_Cfg=ClientOHOH.config.Goods
local rebirthGid = 16 --自动重生

local Tool=require(script.Parent.Tool)


function M.new(self,...)
    return M(self,...)
end

function M.ctor(self,parent)
    M.super.ctor(self,parent)

    self.DataStore = self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())

    self.player = parent


    self:UpdateIsAutoRebirch()
    self:UpdateIsCanAutoRebirch()

    -- self:Update()

    CallBackManager:AddRbxConnect(self,self.player.UserDataEvent, function(obj,data)

        if data["base"]  then
            self:UpdateRebirthUI()
        end

        if data["gamePass"] then
            self:UpdateIsCanAutoRebirch()
        end

        
        if data["set_data"] then
            self:UpdateIsAutoRebirch()
        end
        
    end);

    CallBackManager:AddRbxConnect(self,self.player.LevelChangeEvent, function(obj,data)
        self:Update()
    end);


    CallBackManager:AddRbxConnect(self,HomeGui.insts.Rebirth.button.Activated, function()
        RebirthGui:Show();
    end);
    
    -- --Skips
    -- CallBackManager:AddRbxConnect(self,RebirthGui.insts.BtnSkip.Activated,function()
    --     --跳转
    --     -- local ShopCtrl = self.parent:GetComponent(_G.CommonOHOH.load.Common_ShopCtrl())
    --     -- ShopCtrl:Buy(1113,script.Parent.Name,"BuyDone")
    --     self.parent.RobuxBuyEvent:Action(rebirthGid,Good_Cfg[rebirthGid].gType)
    -- end)

    -- 自动重生
    CallBackManager:AddRbxConnect(self,RebirthGui.insts.BtnSkip.Activated,function()
        --跳转
        -- local ShopCtrl = self.parent:GetComponent(_G.CommonOHOH.load.Common_ShopCtrl())
        -- ShopCtrl:Buy(1113,script.Parent.Name,"BuyDone")
        if not self.IsCanAutoRebirch then
            self.parent.RobuxBuyEvent:Action(rebirthGid,Good_Cfg[rebirthGid].gType)
        else
            self:AddProcess(function()
                local isSuccess,code = self:Invoke(FunctionList.change_auto_rebirch)
                if isSuccess then
                    GlobalTipGui:Show(LanguageHelper:GetText(4))
                end
            end)
         

        end
        
    end)




    --重生
    CallBackManager:AddRbxConnect(self,RebirthGui.insts.BtnRebirth.Activated,function()


        if not self:IsLevelCanRebirth() then
            GlobalTipGui:Show(LanguageHelper:GetText(999001))
            return 
        end

        if not self:IsHadEnough() then
            GlobalTipGui:Show(LanguageHelper:GetText(999002))
            return 
        end

        self:Rebirth()

    end)
     Tool.AddBoost(self.player,self.DataStore)


    -- --重生给经验提供加成
    -- self.player.ExpValueMul2Event:Connect(function(gid,gtype,...)
    --         local cfg=Rebirth_Cfg[self.DataStore:GetData("base").rebirth]
    --         return cfg.boost+1
    -- end)


end

function M.UpdateIsAutoRebirch(self)
    if  self.DataStore:GetData("set_data").isAutoRebirth==1 then
        self.IsAutoRebirth=true
        RebirthGui:SetAutoSate(LanguageHelper:GetText(888062))

        if  self:isCanRebirth() then
            self:Rebirth()
        end   

    else
        self.IsAutoRebirth=false
        RebirthGui:SetAutoSate(LanguageHelper:GetText(888063))
    end
end

function M.UpdateIsCanAutoRebirch(self)
    if  self:IsHadAutoRebirch() then
        self.IsCanAutoRebirch=true
    else
        self.IsCanAutoRebirch=false

    end
end


--判断是否存在自动重生通行证
function M.IsHadAutoRebirch(self)
  local itemData=  Good_Cfg[rebirthGid].itemData
  local data= self.DataStore:GetData("gamePass")
  for k,v in pairs(itemData) do
        if  data[k] then
            return  true
        end
  end
  return  false
end


-- 是否能够重生
function M.isCanRebirth(self)
    return Tool.isCanRebirth(self.DataStore)
end


-- 是否达到重生等级
function M.IsLevelCanRebirth(self)
    return Tool.IsLevelCanRebirth(self.DataStore)
end

-- 是否达到重生等级
function M.IsHadEnough(self)
    return Tool.IsHadEnough(self.DataStore)
end


function M.Update(self)

    if  self:isCanRebirth() then
        if self.IsAutoRebirth then
            self:Rebirth()
        else
            -- RebirthGui:Show()

            ClientOHOH.load.UIManager():PushShow("RebirthGuiNew")


        end
       
    end   
end

-- 发起重生
function M.Rebirth(self)
        --请求服务器判断
        --返回code为0则等级不够，为1则数量不够
        local isSuccess,code = self:Invoke(FunctionList.check_rebirth_data)
        if isSuccess then
            return
        else
            if code == 0 then
                GlobalTipGui:Show(LanguageHelper:GetText(999001))
            else if code == 1 then
                GlobalTipGui:Show(LanguageHelper:GetText(999002))
                end
            end
        end
end



function M.UpdateRebirthUI(self)

    local rebirth = self.DataStore:GetData("base").rebirth

    local nowCfg=Rebirth_Cfg[rebirth]
    if not nowCfg then
        RebirthGui:SetMax(rebirth,nil,Tool.GetMaxBoost())
        return 
    end

    local nextCfg=Rebirth_Cfg[rebirth+1]
    if not nextCfg then
        RebirthGui:SetMax(rebirth,nil,Tool.GetMaxBoost())
        return 
    end

    local titleOldId=nowCfg["titleId"]
    local TitleNewID=nextCfg["titleId"]
    local boostOldValue=nowCfg["boost"]
    local BoostNewValue=nextCfg["boost"]

    RebirthGui:UpdateUI(rebirth,titleOldId,boostOldValue,TitleNewID,BoostNewValue)


    local requiredLevel = nowCfg["requiredLevel"]
    local afterCsumLevel = nowCfg["afterCsumLevel"]
    local currentLevel =self.DataStore:GetData("base").level
    RebirthGui:UpdateLevelUI(requiredLevel,afterCsumLevel,currentLevel)


    local itemKey,count=Tool.GetCostItemRebirthtData(self.DataStore)

    if not itemKey then
        return
    end

    local key,id = ItemTool.GetKeyAndID(itemKey)
    local hasItemAmount = self.DataStore:GetData(key)[id]
    local image=ItemTool.GetItemShowView(itemKey)
    RebirthGui:UpdateProgressUI(count,hasItemAmount,image)


end



function M.ClientBuy(self,gid,gtype,...)
    -- if self:Check(gid,gtype) then
    --     return self:Invoke(FunctionList.buy_goods,gid,...)
    -- else
    --     return false
    -- end
    -- print("客户端处理")
    self:UpdateRebirthUI()
end

--服务器验证购买完成，处理发放逻辑
-- function M.ServerBuyDone(self,gid,...)
-- 	-- local cfg=_G.CommonOHOH.config.Goods[gid]
--     -- for k,v in pairs(cfg.itemData) do
--     --     ItemTool.SaveData(k,self.DataBase,v)	
--     -- end
-- 	-- self.DataBase:Execut(function()
-- 	-- end);
-- 	-- self.parent:SynchDataToClient("buff")
--     print("服务器端处理")
--     self:Invoke(FunctionList.rebirth_buy_done)
-- end

function M.dtor(self)
    M.super.dtor(self)
end

return M 